﻿@model Amazon.Checkout.ViewModels.PromotionNewUpdateViewModel

@{ Script.Require("JQuery").AtHead(); }

<h1>@T((Model.PromotionId == null || Model.PromotionId == 0) ? "New Promotion" : "Edit Promotion")</h1>

@using (Html.BeginFormAntiForgeryPost())
{
    if (!Model.FixedAmountDiscount.HasValue && !Model.DiscountRate.HasValue)
    {
        Model.DiscountRate = 0;
    }

    var discountRateStyle = (Model.PromotionType == null || Model.PromotionType != "FixedAmountDiscount") ? "" : "display:none";    
    var fixedDiscountStyle = Model.PromotionType == "FixedAmountDiscount" ? "" : "display:none";
        
    if (Model.PromotionId != null)
    {
        @Html.HiddenFor(m => m.PromotionId)
    }
    
    @Html.ValidationSummary()
    <fieldset>
        @Html.LabelFor(m => m.PromotionCode, T("Promotion Code:")) 
        @Html.TextBoxFor(m => m.PromotionCode, new { @class = "text" })

        <div>
            @Html.LabelFor(m => m.Description, T("Description:")) 
            @Html.TextAreaFor(m => m.Description, 5, 200, new { @class = "text", @style = "width:99%" })
        </div>

        <label for="PromotionType">@T("Promotion Type:")</label>
        @Html.RadioButton("promotionType", "DiscountRate", Model.DiscountRate.HasValue, new { id = "radio-discount-rate" }) <span style="vertical-align:top;">Discount Rate</span>
        @Html.RadioButton("promotionType", "FixedAmountDiscount", Model.FixedAmountDiscount.HasValue, new { id = "radio-fixed-amount-discount" })<span style="vertical-align:top;">Fixed Amount Discount</span>

        <div id="discount-rate" style="@discountRateStyle">
            @Html.LabelFor(m => m.DiscountRate, T("Discount Rate:")) 
            @Html.TextBoxFor(m => m.DiscountRate, new { @class = "text" }) <em>(0.000001 to 0.15)</em>
        </div>

        <div id="fixed-amount-discount" style="@fixedDiscountStyle">
            @Html.LabelFor(m => m.FixedAmountDiscount, T("Fixed Amount Discount:")) 
            @Html.TextBoxFor(m => m.FixedAmountDiscount, new { @class = "text" })
        </div>
    </fieldset>
    
    <fieldset>
        <button class="primaryAction" type="submit">@T((Model.PromotionId == null || Model.PromotionId == 0) ? "Save" : "Update")</button>
    </fieldset>
}

<script type="text/javascript">
    $(document).ready(function () {
        $("input[type='radio']").each(function () {
            $(this).change(function () {
                if ($(this).val() == "FixedAmountDiscount") {
                    $("#discount-rate").hide();
                    $("#fixed-amount-discount").show();
                }
                else {
                    $("#discount-rate").show();
                    $("#fixed-amount-discount").hide();
                }
            });
        })
    });
</script>
